import { defineStore } from 'pinia'
import { ref } from 'vue'
export const useinfoStore = defineStore('info', () => {
    const query = ref(localStorage.getItem('query') || 0)
    const cost = ref(localStorage.getItem('cost') || 0)
    const imgNum = ref(localStorage.getItem('imgNum') || 0)
    const his_cook = ref([])
    const his_doc = ref([])
    const his_help = ref([])
    const his_img = ref([])

    function save(data,module) {
        if(module == 'cooker'){
            his_cook.value.push(data)
            localStorage.setItem('his_cook',JSON.stringify(his_cook.value))
        }else if(module == 'doctor'){
            his_doc.value.push(data)
            localStorage.setItem('his_doc',JSON.stringify(his_doc.value))
        }else if(module == 'helper'){
            his_help.value.push(data)
            localStorage.setItem('his_help',JSON.stringify(his_help.value))
        }else if(module == 'imgCreate'){
            his_img.value.push(data)
            localStorage.setItem('his_img',JSON.stringify(his_img.value))
        }
    }
    function init() {
        his_cook.value = JSON.parse(localStorage.getItem('his_cook') || '[]')
        his_doc.value = JSON.parse(localStorage.getItem('his_doc') || '[]')
        his_help.value = JSON.parse(localStorage.getItem('his_help') || '[]')
        his_img.value = JSON.parse(localStorage.getItem('his_img') || '[]')
    }
    
    function addQuery() {
        query.value++
        localStorage.setItem('query',query.value)
    }

    function addCost(num) {
        cost.value=parseInt(cost.value)+parseInt(num)
        localStorage.setItem('cost',cost.value)
    }

    function addImgNum(num) {
        imgNum.value=parseInt(num)+parseInt(imgNum.value)
        localStorage.setItem('imgNum',imgNum.value)
    }

    return {
        query,
        cost,
        imgNum,
        addQuery,
        addCost,
        addImgNum,
        his_cook,
        his_doc,
        his_help,
        his_img,
        init,
        save,
    }
})